package com.zust513.service.impl;

import com.zust513.dto.ExpenseDTO;
import com.zust513.dto.IncomeDTO;
import com.zust513.mapper.FinanceMapper;
import com.zust513.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 财务服务实现类
 */
@Service
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    private FinanceMapper financeMapper;

    @Override
    public List<IncomeDTO> getAllIncomeRecords() {
        List<Map<String, Object>> incomeRecords = financeMapper.findAllIncomeRecords();
        List<IncomeDTO> incomeDTOs = new ArrayList<>();

        for (Map<String, Object> record : incomeRecords) {
            IncomeDTO dto = mapToIncomeDTO(record);
            incomeDTOs.add(dto);
        }

        return incomeDTOs;
    }

    @Override
    public BigDecimal getTotalIncome() {
        return financeMapper.getTotalIncome();
    }

    @Override
    public List<ExpenseDTO> getAllCourierExpenses() {
        List<Map<String, Object>> expenseRecords = financeMapper.findAllCourierExpenses();
        List<ExpenseDTO> expenseDTOs = new ArrayList<>();

        for (Map<String, Object> record : expenseRecords) {
            ExpenseDTO dto = mapToExpenseDTO(record);
            expenseDTOs.add(dto);
        }

        return expenseDTOs;
    }

    @Override
    public List<ExpenseDTO> getCourierExpensesByCourierId(Integer courierId) {
        List<Map<String, Object>> expenseRecords = financeMapper.findCourierExpensesByCourierId(courierId);
        List<ExpenseDTO> expenseDTOs = new ArrayList<>();

        for (Map<String, Object> record : expenseRecords) {
            ExpenseDTO dto = mapToExpenseDTO(record);
            expenseDTOs.add(dto);
        }

        return expenseDTOs;
    }

    @Override
    public BigDecimal getTotalExpense() {
        return financeMapper.getTotalExpense();
    }

    /**
     * 将Map转换为IncomeDTO
     */
    private IncomeDTO mapToIncomeDTO(Map<String, Object> map) {
        IncomeDTO dto = new IncomeDTO();

        // 安全处理finance_id的类型转换
        Object financeId = map.get("finance_id");
        if (financeId instanceof Long) {
            dto.setFinanceId(((Long) financeId).intValue());
        } else if (financeId instanceof Integer) {
            dto.setFinanceId((Integer) financeId);
        }

        dto.setAmount((BigDecimal) map.get("amount"));

        // 安全处理order_id的类型转换
        Object orderId = map.get("order_id");
        if (orderId instanceof Long) {
            dto.setOrderId(((Long) orderId).intValue());
        } else if (orderId instanceof Integer) {
            dto.setOrderId((Integer) orderId);
        }

        dto.setDescription((String) map.get("description"));
        dto.setOrderAddress((String) map.get("order_address"));
        dto.setCustomerName((String) map.get("customer_name"));
        dto.setCreatedAt((LocalDateTime) map.get("created_at"));
        return dto;
    }

    /**
     * 将Map转换为ExpenseDTO
     */
    private ExpenseDTO mapToExpenseDTO(Map<String, Object> map) {
        ExpenseDTO dto = new ExpenseDTO();

        // 安全处理transaction_id的类型转换
        Object transactionId = map.get("transaction_id");
        if (transactionId instanceof Long) {
            dto.setTransactionId(((Long) transactionId).intValue());
        } else if (transactionId instanceof Integer) {
            dto.setTransactionId((Integer) transactionId);
        }

        dto.setAmount((BigDecimal) map.get("amount"));

        // 安全处理courier_id的类型转换
        Object courierId = map.get("courier_id");
        if (courierId instanceof Long) {
            dto.setCourierId(((Long) courierId).intValue());
        } else if (courierId instanceof Integer) {
            dto.setCourierId((Integer) courierId);
        }

        dto.setCourierName((String) map.get("courier_name"));

        // 安全处理order_id的类型转换
        Object orderId = map.get("order_id");
        if (orderId instanceof Long) {
            dto.setOrderId(((Long) orderId).intValue());
        } else if (orderId instanceof Integer) {
            dto.setOrderId((Integer) orderId);
        }

        dto.setOrderAddress((String) map.get("order_address"));
        dto.setCreatedAt((LocalDateTime) map.get("created_at"));
        dto.setDescription((String) map.get("description"));
        return dto;
    }
}
